package com.handsome.service.impl;

import com.github.pagehelper.PageHelper;
import com.handsome.annotations.ActionLogAnnotation;
import com.handsome.dao.HsMemberMapper;
import com.handsome.dao.HsRoleMapper;
import com.handsome.dto.BootstrapDataTableDto;
import com.handsome.entity.HsLog;
import com.handsome.entity.HsMember;
import com.handsome.entity.HsRole;
import com.handsome.entity.HsWidget;
import com.handsome.service.MemberService;
import com.handsome.service.dto.MemberDto;
import com.handsome.utils.Md5Util;
import com.handsome.utils.Result;
import com.handsome.utils.ResultGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {


    @Autowired
    private HsMemberMapper hsMemberMapper;

    @Autowired
    private HsRoleMapper hsRoleMapper;

    @ActionLogAnnotation(module = "member", description = "登录后台系统")
    @Override
    public Result loginByPassword(HsMember memberEntity) {
        String password = memberEntity.getPassword();
        memberEntity.setPassword(null);
        String salt;
        try {
            salt = hsMemberMapper.selectOne(memberEntity).getSalt();
        } catch (NullPointerException e) {
            return ResultGenerator.genFailResult("账号不存在");
        }

        memberEntity.setPassword(Md5Util.md5(password + salt, true, 32));
        HsMember hsMember = hsMemberMapper.selectOne(memberEntity);
        if (hsMember == null) {
            return ResultGenerator.genFailResult("密码错误");
        }
        return ResultGenerator.genSuccessResult(hsMember);
    }

    @Override
    public BootstrapDataTableDto getMemberList() {


        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int offset = Integer.parseInt(request.getParameter("offset"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String search = request.getParameter("search");
        BootstrapDataTableDto<HsMember> tableDto = new BootstrapDataTableDto<>();
        int count = hsMemberMapper.selectCount(new HsMember());
        tableDto.setTotal(count);
        PageHelper.offsetPage(offset, limit);

        List<HsMember> memberList = hsMemberMapper.getMemberList(search);

        tableDto.setRows(memberList);
        return tableDto;


    }

    @Override
    public BootstrapDataTableDto getRoles() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int offset = Integer.parseInt(request.getParameter("offset"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String search = request.getParameter("search");
        BootstrapDataTableDto<HsRole> tableDto = new BootstrapDataTableDto<>();
        int count = hsMemberMapper.selectCount(new HsMember());
        tableDto.setTotal(count);
        PageHelper.offsetPage(offset, limit);
        List<HsRole> roleList = hsRoleMapper.getRoleList();
        tableDto.setRows(roleList);
        return tableDto;
    }
}
